/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/ban-ts-comment */
const fs = require("fs")
const path = require("path")

module.exports = async (
  deployer: Truffle.Deployer,
  network: "testnet" | "local",// npm run migrate --local时network是local
  accounts: string[]
) => {

  // TypeChain不支持生成Library的泛型，因此没有类型提升，但是实际上是有CounterLib的（位于src/build文件夹）
  // @ts-ignore
  const Lib = artifacts.require("CounterLib");

  const Counter = artifacts.require("Counter");
  await deployer.deploy(Lib);

  // Contract调用Library时需要link两个合约
  deployer.link(Lib, Counter)
  await deployer.deploy(Counter);
  const instance = await Counter.deployed();

  // 将合约地址保存到output.json，供main.ts使用
  fs.writeFileSync(path.join(__dirname, "..", "output.json"), JSON.stringify({
    "address": instance.address
  }))
};
